import os

from urllib3 import request

from ..models import Scene
from ..models import Profile
from dvadmin.utils.viewset import CustomModelViewSet
from ...utils.serializers import CustomModelSerializer
from .Helper import *

class SceneSerializer(CustomModelSerializer):
    class Meta:
        model = Scene
        fields = '__all__'

    def create(self, validated_data):
        server_port = self.request.META.get('SERVER_PORT')
        profile_id = validated_data['profile_id']
        if not Profile.objects.filter(id=profile_id).exists():
            raise TypeError("场景依赖的想定文件不存在！")
        profile_name = Profile.objects.get(id=profile_id).name
        profile_path = Profile.objects.get(id=profile_id).path
        # cd_key = (get_local_ip() + ":" + request.META.get('SERVER_PORT') +"/media/"+profile_path)
        # project_path = os.path.join(get_project_path(), "python_project_base", str(uid))
        # if not os.path.exists(project_path):
        #     os.makedirs(project_path)



class SceneViewSet(CustomModelViewSet):
    filter_fields = ["id", "title", "author", "profile_id"]
    queryset = Scene.objects.all()
    serializer_class = SceneSerializer
    create_serializer_class = SceneSerializer
    # update_serializer_class = SceneSerializer
    # retrieve_serializer_class = SceneSerializer
    # delete_serializer_class = SceneSerializer
    # list_serializer_class = SceneSerializer